/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/idenger">idenger</a> All rights reserved.
 */
package com.idenger.modules.project.web.fundmanagement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.idenger.common.config.Global;
import com.idenger.common.persistence.Page;
import com.idenger.common.web.BaseController;
import com.idenger.common.utils.StringUtils;
import com.idenger.modules.project.entity.fundmanagement.FundManagement;
import com.idenger.modules.project.service.fundmanagement.FundManagementService;

import java.util.List;

/**
 * 资金管理Controller
 * @author chenlin
 * @version 2018-04-11
 */
@Controller
@RequestMapping(value = "${adminPath}/project/fundmanagement/fundManagement")
public class FundManagementController extends BaseController {

	@Autowired
	private FundManagementService fundManagementService;
	
	@ModelAttribute
	public FundManagement get(@RequestParam(required=false) String id) {
		FundManagement entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = fundManagementService.get(id);
		}
		if (entity == null){
			entity = new FundManagement();
		}
		return entity;
	}
	
	@RequiresPermissions("project:fundmanagement:fundManagement:view")
	@RequestMapping(value = {"list", ""})
	public String list(FundManagement fundManagement, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<FundManagement> page = fundManagementService.findPage(new Page<FundManagement>(request, response), fundManagement); 
		model.addAttribute("page", page);
		return "modules/project/fundmanagement/fundManagementList";
	}

	@RequiresPermissions("project:fundmanagement:fundManagement:view")
	@RequestMapping(value = "form")
	public String form(HttpServletRequest request,FundManagement fundManagement, Model model) {

		List<FundManagement> fundManagementList =fundManagementService.getFundManagementByProjectId(request.getParameter("projectId"));
		if(fundManagementList.size()>0){
			fundManagement = fundManagementList.get(0);
		}
		model.addAttribute("fundManagement", fundManagement);
		return "modules/project/fundmanagement/fundManagementForm";
	}

	@RequiresPermissions("project:fundmanagement:fundManagement:edit")
	@RequestMapping(value = "save")
	public String save(FundManagement fundManagement, Model model, RedirectAttributes redirectAttributes) {

		fundManagementService.save(fundManagement);
		addMessage(redirectAttributes, "保存资金管理成功");
		return "redirect:"+Global.getAdminPath()+"/project/fundmanagement/fundManagement/?repage";
	}
	
	@RequiresPermissions("project:fundmanagement:fundManagement:edit")
	@RequestMapping(value = "delete")
	public String delete(FundManagement fundManagement, RedirectAttributes redirectAttributes) {
		fundManagementService.delete(fundManagement);
		addMessage(redirectAttributes, "删除资金管理成功");
		return "redirect:"+Global.getAdminPath()+"/project/fundmanagement/fundManagement/?repage";
	}

}